# 第二章

# p76  "Don't panic!" => list => "on tap"
phrase = "Don't panic!"
plist = list(phrase)  # str 转换 list 使用的方法是 list（str）
print(phrase)
print(plist)

for i in range(4):
    # 循环执行4次 删除末位 使用方法 pop（）
    plist.pop()
print(plist)

plist.pop(0)
print(plist)

plist.remove("'")
print(plist)


# extend => plist + [plist.pop(),plist.pop()]
plist.extend([plist.pop(),plist.pop()]) # 运算顺序，小括号优先
# ["o","n","t"]     a            p
print(plist)

plist.insert(2,plist.pop(3))
#                     ' '
print(plist)

new_phrase = ''.join(plist) #? ''.join()   list 转换 str
print("plist=",plist) # list的方法会打乱和改变list的内容，以防万一可以先复制列表

# P78
book = "The Hitchhiker's Guide to the Galaxy"
booklist = list(book)
print(booklist)

booklist[0:3]
''.join(booklist[0:3])
''.join(booklist[-6:])
print(booklist)

# P79
# 从列表末尾开始，步长为1
backwards = booklist[::-1]
''.join(backwards)
print(backwards)

# 每隔一个选择一个字母（对象）
every_other = booklist[::2]
''.join(every_other)
print(every_other)

# 取出单词 Hitchhiker
''.join(booklist[4:14])
# 取出单词 Hitchhiker 并以逆序选择（从后向前）
''.join(booklist[13:3:-1])

# P88 Marvin切片
paranoid_android = "Marvin, the Paranoid Android"
letters = list(paranoid_android)

for char in letters[:6]:
    print('\t', char)
for char in letters[-7:]:
    print('\t'*2, char) # * 表示空格即 后退两格
for char in letters[12:20]:
    print('\t'*3,char)


